package com.zp.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.api.entity.SysBaseDictType;
import com.zp.system.entity.bo.SysBaseDictTypeBo;
import com.zp.system.entity.vo.SysBaseDictTypeVo;
import com.zp.system.mapper.SysBaseDictTypeMapper;
import com.zp.system.service.ISysBaseDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 字典类型Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-01
 */
@RequiredArgsConstructor
@Service
public class SysBaseDictTypeServiceImpl implements ISysBaseDictTypeService {

    private final SysBaseDictTypeMapper baseMapper;

    /**
     * 查询字典类型
     */
    @Override
    public SysBaseDictTypeVo queryById (String baseDictId) {
        return baseMapper.selectVoById(baseDictId);
    }

    /**
     * 查询字典类型列表(分页)
     */
    @Override
    public TableDataInfo<SysBaseDictTypeVo> queryPageList (SysBaseDictTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysBaseDictType> lqw = buildQueryWrapper(bo);
        Page<SysBaseDictTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询字典类型列表(不分页)
     */
    @Override
    public List<SysBaseDictTypeVo> queryList (SysBaseDictTypeBo bo) {
        LambdaQueryWrapper<SysBaseDictType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysBaseDictType> buildQueryWrapper (SysBaseDictTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysBaseDictType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getBaseDictName()), SysBaseDictType::getBaseDictName, bo.getBaseDictName());
        lqw.like(StringUtils.isNotBlank(bo.getDictType()), SysBaseDictType::getDictType, bo.getDictType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysBaseDictType::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增字典类型
     */
    @Override
    public Boolean insertByBo (SysBaseDictTypeBo bo) {
        SysBaseDictType add = BeanUtil.toBean(bo, SysBaseDictType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBaseDictId(add.getBaseDictId());
        }
        return flag;
    }

    /**
     * 修改字典类型
     */
    @Override
    public Boolean updateByBo (SysBaseDictTypeBo bo) {
        SysBaseDictType update = BeanUtil.toBean(bo, SysBaseDictType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (SysBaseDictType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除字典类型
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
